package cn.lsnu.modules.login.service;

import cn.lsnu.common.interfacetools.interfaceservice.BaseService;
import cn.lsnu.modules.login.entity.LoginEntity;
import cn.lsnu.modules.login.mapper.LoginMapper;
import cn.lsnu.utils.Global;
import cn.lsnu.utils.Guuid;
import cn.lsnu.utils.MD5;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

@Service("loginService")
public class LoginService extends BaseService<LoginEntity,LoginMapper> {
    @Override
    public LoginEntity getEntity(LoginEntity entity) throws Exception {

        return super.getEntity(entity);
    }
    @Override
    public void save(LoginEntity loginEntity) throws Exception {
        loginEntity.setUserId(Guuid.uuid());
        //密码验证
//        loginEntity.setUserPwd(MD5.md5(loginEntity.getUserPwd()));
        super.save(loginEntity);
    }
    public Integer loginVerfication(LoginEntity liEntity,HttpSession session) throws Exception {
        LoginEntity dbEntity =   getEntity(liEntity);
       if(liEntity == null){
            return Global.UNKNOWN_ERROR;
       }
       if(dbEntity != null){
           if(dbEntity.getUserStatus() != 1){
                return Global.LOGIN_USER_NO_AUTHORIZATION;
           }
//           if(!dbEntity.getUserPwd().equals(MD5.md5(dbEntity.getUserPwd()))){
           if(!dbEntity.getUserPwd().equals(liEntity.getUserPwd())){
               return  Global.LOGIN_PASSWARD_ERROR;
           }
       }else{
          return Global.LOGIN_USER_NO_EXISTENCE;
       }
        session.removeAttribute("userName");
        session.setAttribute("userName",dbEntity.getUserName());
        return Global.LOGIN_SUCEESS;
    }

    public String loginDealResult(LoginEntity loginEntity, HttpSession session) throws Exception {
        String msg = "";
        String resultCode = Global.LOGIN_SUCCESS_STRING;

        //验证码验证
//        resultCode  = verifyCodeService(loginEntity,session);
        if(!resultCode.trim().equalsIgnoreCase(Global.LOGIN_SUCCESS_STRING.trim())){
            return  resultCode;
        }
        Integer result = loginVerfication(loginEntity,session);
        if(result == Global.LOGIN_USER_NO_AUTHORIZATION){
            return "此账号未授权！";
        }
        if (result == Global.LOGIN_USER_NO_EXISTENCE)
            msg = "此账号不存在";
        else if (result == Global.LOGIN_SUCEESS) {
            session.removeAttribute(Global.SESSION_ID);
            session.setAttribute(Global.SESSION_ID,loginEntity.getUserCode());
            msg = "SUCCESS";
        }
        else if(result == Global.LOGIN_PASSWARD_ERROR)
            msg= "密码错误";
        else if(result == Global.LOGIN_DATABASE_ERROR)
            msg = "数据库错误";
        else
            msg= "未知错误";
        return msg;
    }

    public String verifyCodeService(LoginEntity loginEntity, HttpSession session){
        String verifyCodeSesion = (String) session.getAttribute(Global.RANDOMCODEKEY);
        if(loginEntity.getverifyCode()==null || loginEntity.getverifyCode().isEmpty())
            return  "验证码不能为空";
        if(!verifyCodeSesion.trim().toString().equalsIgnoreCase(loginEntity.getverifyCode().trim().toString()))
            return "验证码错误";
        return "SUCCESS";
    }





}
